package dao

import (
	"gorm.io/gorm"
	"kf/models/entity"
	"time"
)

type VisitorsDao struct {
	DB *gorm.DB
}

func (visitorsDao *VisitorsDao) CreateVisitor(name, avator, sourceIp, toId, visitorId, refer, city, clientIp, extra string) {
	v := &entity.Visitor{
		Name:      name,
		Avator:    avator,
		SourceIp:  sourceIp,
		ToId:      toId,
		VisitorId: visitorId,
		Status:    1,
		Refer:     refer,
		City:      city,
		ClientIp:  clientIp,
		Extra:     extra,
	}
	v.UpdatedAt = time.Now()
	visitorsDao.DB.Create(v)
}

// 根据id查找
func (visitorsDao *VisitorsDao) FindVisitorByVisitorId(visitorId string) entity.Visitor {
	var v entity.Visitor
	visitorsDao.DB.Where("visitor_id = ?", visitorId).First(&v)
	return v
}
func (visitorsDao *VisitorsDao) FindVisitors(page uint, pagesize uint) []entity.Visitor {
	offset := (page - 1) * pagesize
	if offset < 0 {
		offset = 0
	}
	var visitors []entity.Visitor
	visitorsDao.DB.Offset(int(offset)).
		Limit(int(pagesize)).
		Order("status desc, updated_at desc").Find(&visitors)
	return visitors
}
func (visitorsDao *VisitorsDao) FindVisitorsByKefuId(page uint,
	pagesize uint,
	kefuId string,
	visitorId string) []entity.VisitorPageVo {
	offset := (page - 1) * pagesize
	if offset <= 0 {
		offset = 0
	}
	var visitors []entity.VisitorPageVo
	//sql := fmt.Sprintf("select * from visitor where id>=(select id from visitor where  to_id='%s' order by updated_at desc limit %d,1) and to_id='%s' order by updated_at desc limit %d ", kefuId, offset, kefuId, pagesize)
	//visitorsDao.DB.Raw(sql).Scan(&visitors)
	tx := visitorsDao.DB.Model(&entity.Visitor{}).
		Select("visitor.*,user.nickname").
		Joins("left join sys_user as user on visitor.to_id=user.id")
	if kefuId != "" {
		tx.Where("to_id=?", kefuId)
	}
	if visitorId != "" {
		tx.Where("visitor_id=?", visitorId)
	}
	tx.Offset(int(offset)).Limit(int(pagesize)).
		Order("updated_at desc").Find(&visitors)
	for i, m := range visitors {
		visitors[i].CreateTime = m.CreatedAt.Format("2006-01-02 15:04:05")
	}
	return visitors
}
func (visitorsDao *VisitorsDao) FindVisitorsOnline() []entity.Visitor {
	var visitors []entity.Visitor
	visitorsDao.DB.Where("status = ?", 1).Find(&visitors)
	return visitors
}
func (visitorsDao *VisitorsDao) UpdateVisitorStatus(visitorId string, status uint) {
	visitor := entity.Visitor{}
	visitorsDao.DB.Model(&visitor).Where("visitor_id = ?", visitorId).Update("status", status)
}
func (visitorsDao *VisitorsDao) UpdateVisitor(name, avator, visitorId string, status uint, clientIp string, sourceIp string, refer, extra string, kefuId string) {
	visitor := &entity.Visitor{
		Status:   status,
		ClientIp: clientIp,
		SourceIp: sourceIp,
		Refer:    refer,
		Extra:    extra,
		Name:     name,
		Avator:   avator,
		ToId:     kefuId,
	}
	visitor.UpdatedAt = time.Now()
	visitorsDao.DB.Model(visitor).Where("visitor_id = ?", visitorId).Updates(visitor)
}

func (visitorsDao *VisitorsDao) UpdateVisitorKefu(visitorId string, kefuId string) {
	visitor := entity.Visitor{}
	visitorsDao.DB.Model(&visitor).Where("visitor_id = ?", visitorId).Update("to_id", kefuId)
}

// 查询条数
func (visitorsDao *VisitorsDao) CountVisitors() int64 {
	var count int64
	visitorsDao.DB.Model(&entity.Visitor{}).Count(&count)
	return count
}

func (visitorsDao *VisitorsDao) UpdateVisitorModel(visitor *entity.Visitor) {
	visitorsDao.DB.Model(&entity.Visitor{}).Updates(visitor)
}

// 查询条数
func (visitorsDao *VisitorsDao) CountVisitorsByKefuId(kefuId string, visitorId string) int64 {
	var count int64
	tx := visitorsDao.DB.Model(&entity.Visitor{})
	if kefuId != "" {
		tx.Where("to_id=?", kefuId)
	}
	if visitorId != "" {
		tx.Where("visitor_id = ?", visitorId)
	}
	tx.Count(&count)
	return count
}

// 查询每天条数
type EveryDayNum struct {
	Day string `json:"day"`
	Num int64  `json:"num"`
}

func (visitorsDao *VisitorsDao) CountVisitorsEveryDay(toId string) []EveryDayNum {
	var results []EveryDayNum
	visitorsDao.DB.Raw("select DATE_FORMAT(created_at,'%y-%m-%d') as day ,"+
		"count(*) as num from visitor where to_id=? group by day order by day desc limit 30",
		toId).Scan(&results)
	return results
}

func (visitorsDao *VisitorsDao) DeleteBind(id string) error {
	err := visitorsDao.DB.Model(&entity.Visitor{}).Where("id = ?", id).
		Update("to_id", "").Error
	return err
}
